package com.xy.servicemanage.request;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.gson.GsonBuilder;
import com.xy.servicemanage.action.TservicesAction;
import com.xy.servicemanage.model.ServiceSetting;
import com.xy.servicemanage.model.Tresparams;
import com.xy.servicemanage.model.Tservices;

public class RequestHelper {
	private static String errorCode = "errorCode";
	private static String errorMeesage = "errorMessage";
	private static String serviceId = "serviceId";
	private static String custId = "custId";
	private static String signMsg = "signMsg";
	private static String sendTime = "SendTime";
	private static String Total = "total";
	//违章查询验证输入接口
	private static String VAL_BR_input = "10013";
	private static final Logger log = LoggerFactory
			.getLogger(RequestHelper.class);

	public static String processRequest(Tservices service,
			Map<String, String> vmap) {

		try {
			Object ret = null;
			String serviceid = vmap.get(TservicesAction.serviceId);

			if ("1030".equals(serviceid)) {
				// 发送短信验证码
				String code = VerifyCodeHelper.sendMessage(vmap);
				List<Tresparams> resList = ServiceSetting.getInstance()
						.getResparamBySid(service.getSid());
				Map m = new HashMap();
				m.put(resList.get(0).getResid(), code);
				List<Map> l=new ArrayList<Map>();
				l.add(m);
				ret =l;
			} else if ("1019".equals(serviceid)
					&& "0".equals(vmap.get("LoginType"))) {
				// 短信验证码登陆
				int r = VerifyCodeHelper.verify(vmap);
				List<Tresparams> resList = ServiceSetting.getInstance()
						.getResparamBySid(service.getSid());
				Map m = new HashMap();
				m.put(resList.get(0).getResid(), r);
				List<Map> l=new ArrayList<Map>();
				l.add(m);
				ret = l;
			} else if ("1028".equals(serviceid)) {
				Tservices testService =  ServiceSetting.getInstance().getServiceByServiceId(VAL_BR_input);
				ret = ServiceEnginer.getInstance().process(testService, vmap);
				List<Map> list = (List<Map>) ret;
				for (Map m : list) {
					if (null != m.get(Total) && ((Integer) m.get(Total)) == 0) {
						String str = new GsonBuilder().create().toJson(ret);
						StringBuffer sb = new StringBuffer();
						sb.append("{\"returnCode\":\"1\",\"returnMessage\":\"success\",\"result\":[{\"result\":\"输入错误\"}]}");
						return sb.toString();
					}
				}
				ret = ServiceEnginer.getInstance().process(service, vmap);
			} else {
				ret = ServiceEnginer.getInstance().process(service, vmap);
			}
			String str = new GsonBuilder().create().toJson(ret);
			StringBuffer sb=new StringBuffer();
			sb.append("{\"returnCode\":\"1\",\"returnMessage\":\"success\",\"result\":");
			sb.append(str).append("}");
			return sb.toString();
		} catch (Exception e) {
			log.error("processRequest", e);
			return getError("10007", e.getLocalizedMessage());
		}
 
	}
	
	 

	private static String getError(String code, String msg)  {
		Map<String, Object> m = new HashMap<String, Object>();
		m.put(returnCode, code);
		m.put(returnMessage, msg);
		m.put(result, new ArrayList());
		return new GsonBuilder().create().toJson(m);
	}
	public static String returnCode = "returnCode";
	public static String returnMessage = "returnMessage";
	public static String result = "result";
	
}
